package se.lcsmerge.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.Vector;

import javax.swing.JOptionPane;

import se.lcsmerge.data.FileDataDTO;


/**
 * 
 * @author caucse06 SE Team
 *
 */
public class FileIOImpl implements FileIO {

		
	/**
	 * @param String
	 * @author yj
	 */
	@Override
	public Vector<FileDataDTO> readPerLine(String fileLocation) throws FileNotFoundException {
		// TODO Auto-generated method stub
		
		/* definition field */
		File r = new File(fileLocation);
	    Vector<FileDataDTO> v = new Vector<FileDataDTO>();
	    BufferedReader in = null;
	    String str = null;
	    
		if (!r.isFile() || !r.exists()) throw new FileNotFoundException("Cannot read requested file data.");
       
	    try {
	    	/* logic field */
	    	in = new BufferedReader(new FileReader(r));
	            	        
	        while ((str = in.readLine()) != null) {
	        	FileDataDTO dto = new FileDataDTO();
	        	if (str.length() < 100)
	            	dto.setString(str.substring(0));
	            else
	            	dto.setString(str.substring(0, 100));
	        	v.add(dto);
	        }
	        
	        return v;
	        
	    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
	        if (in != null)
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	    }
		return v;
	}

/**
* @param String, Vector<FileDataDTO>
* @author yj
 * @throws FileNotFoundException 
*/
	@Override
	public void writePerLine(String fileLocation, Vector<FileDataDTO> v) throws FileNotFoundException {
		// TODO Auto-generated method stub
		/* definition field */
		File r = new File(fileLocation);
	    BufferedWriter out = null;
		if (!r.isFile() || !r.exists()) throw new FileNotFoundException("Cannot read requested file data.");

		try {
	    	
	    	/* logic field */
	    	
	    	out = new BufferedWriter(new FileWriter(fileLocation));
	    	
	    	for(int i=0; i<v.size(); i++){
	    		out.write((String)v.get(i).getString());
	    		out.newLine();
	    	}

    	        
	    } catch(FileNotFoundException e){
	    	
	    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				out.flush();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }	
	}
/**
 * @param String, String
 * @author yj
 * @throws FileNotFoundException 
 */
	@Override
	public Vector<FileDataDTO> writePerLine(String fileLocation, String data) throws FileNotFoundException{
		Vector<FileDataDTO> v = new Vector<FileDataDTO>();
		String oneLine;
		BufferedReader in = null;

		in = new BufferedReader(new StringReader(data));

			try {
				while ((oneLine = in.readLine()) != null) {
					FileDataDTO dto = new FileDataDTO();
					if (oneLine.length() < 100)
				    	dto.setString(oneLine.substring(0));
				    else
				    	dto.setString(oneLine.substring(0, 100));
					v.add(dto);
					
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "File can not found");
			}
	
		this.writePerLine(fileLocation, v);
		return v;
		
	}
}
